package TakeAwaySystem.adminoperation.admin;

import TakeAwaySystem.Admin;
import TakeAwaySystem.adminoperation.AdminIOperation;
import TakeAwaySystem.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/27 10:57:42
 */
public class AdminDeleteOperation implements AdminIOperation {
    @Override
    public void work(Admin o, File file) {
        System.out.println("删除商户 请输入密码");
        Scanner scanner = new Scanner(System.in);
        List<User> userList = fileToList(file);
        User toget = null;
        for(User user : userList){
            if(user.getId() == ((Admin)o).getId()){
                System.out.println("你有三次输入这个密码 否则退出系统");
                int count = 3;
                String password = null;
                do{
                    password = scanner.next();
                }while(count > 0 && !password.equals(((Admin)user).getPassword()));
                if(count <= 0){
                    System.out.println("退出系统");
                    System.exit(0);
                }
                toget = user;
            }
        }
        userList.remove(toget);
        ListToFile(userList,file);
        System.exit(0);
    }

    @Override
    public List<User> fileToList(File file)  {
        List<User> list = new ArrayList<>();
        //File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while(true){
                String str = br.readLine();
                if(str != null){
                    String[] arr = str.split(" ");
                    Admin users = new Admin();
                    users.setId(Integer.valueOf(arr[0]));
                    users.setPassword(arr[1]);
                    users.setName(arr[2]);
                    list.add(users);
                }else{
                    break;
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }
    @Override
    public void ListToFile(List<User> list,File file) {
        // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for(int i = 0;i<N;i++){
                Admin user = (Admin) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(user.getId() + " ");
                sbr.append(user.getPassword() + " ");
                sbr.append(user.getName() + " ");
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }
}
